import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Pattern;

public class DateUtil {

    public static final Long OneDayTimeNumber = 24 * 60 * 60 * 1000L;

    public static Date convertToDate(String dateStr) throws Exception {
        if (StringUtils.isNotBlank(dateStr)) {
            if (dateStr.contains("/")) {
                dateStr = dateStr.replaceAll("/", "-");
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                return simpleDateFormat.parse(dateStr);
            } else {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                return simpleDateFormat.parse(dateStr);
            }
        }
        return null;
    }

    public static String convertToStrSimple(Date date, String simpleDateFormat) {
        String str = "";
        try {
            SimpleDateFormat format = new SimpleDateFormat(simpleDateFormat);
            if (date != null) {
                str = format.format(date);
            }
        } catch (Exception e) {
            return str;
        }
        return str;
    }

    public static Date convertToDate(Long date) throws Exception {
        return new Date(date);
    }

    public static Date convertToDate(String dateStr, String format) throws Exception {
        if (StringUtils.isNotBlank(dateStr)) {
            if (dateStr.contains("/")) {
                dateStr = dateStr.replaceAll("/", "-");
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
                return simpleDateFormat.parse(dateStr);
            } else {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
                return simpleDateFormat.parse(dateStr);
            }
        }
        return null;
    }

    public static String convertToStr(Date date) {
        String str = "";
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if (date != null) {
                str = format.format(date);
            }
        } catch (Exception e) {
            return str;
        }
        return str;
    }

    public static String convertToStr2(Date date) {
        String str = "";
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
            if (date != null) {
                str = format.format(date);
            }
        } catch (Exception e) {
            return str;
        }
        return str;
    }

    public static String convertToYMD(Date date) {
        String str = "";
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            if (date != null) {
                str = format.format(date);
            }
        } catch (Exception e) {
            return str;
        }
        return str;
    }

    public static String convertToStr(Date date, String formatStr) {
        String str = "";
        try {
            SimpleDateFormat format = new SimpleDateFormat(formatStr);
            if (date != null) {
                str = format.format(date);
            }
        } catch (Exception e) {
            return str;
        }
        return str;
    }


    /**
     * 当前时间加上秒数获取到期时间
     */
    public static Date addSecond(Date startDate, Integer seconds) {
        try {
            if (seconds != null) {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(startDate);
                calendar.add(Calendar.SECOND, seconds);
                return calendar.getTime();
            } else {
                return null;
            }
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 当前时间加上秒数获取到期时间
     */
    public static Date addMonth(Date startDate, Integer Months) {
        try {
            if (Months != null && startDate != null) {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(startDate);
                calendar.add(Calendar.MONTH, Months);
                return calendar.getTime();
            } else {
                return null;
            }
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 当前时间加上小时获取到期时间
     */
    public static Date addHours(Date startDate, Integer hours) {
        try {
            if (hours != null && startDate != null) {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(startDate);
                calendar.add(Calendar.HOUR, hours);
                return calendar.getTime();
            } else {
                return startDate;
            }
        } catch (Exception e) {
            return startDate;
        }
    }

    /**
     * 当前时间加上小时数
     */
    public static Date addHours(Integer hours) {
        return addHours(new Date(), hours);
    }


    /**
     * 当前时间加上天数获取到期时间
     */
    public static Date addDays(Date startDate, Integer days) {
        try {
            if (days != null && startDate != null) {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(startDate);
                calendar.add(Calendar.DAY_OF_MONTH, days);
                return calendar.getTime();
            } else {
                return startDate;
            }
        } catch (Exception e) {
            return startDate;
        }
    }

    /**
     * 获取零点时间
     *
     * @param flag 0：获取当天0点，1：获取当天23:59:59
     */
    public static Date getZeroTime(Date date, int flag) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        int minute = cal.get(Calendar.MINUTE);
        int second = cal.get(Calendar.SECOND);
        //时分秒（毫秒数）
        long millisecond = hour * 60 * 60 * 1000 + minute * 60 * 1000 + second * 1000;
        //凌晨00:00:00
        cal.setTimeInMillis(cal.getTimeInMillis() - millisecond);

        if (flag == 0) {
            return cal.getTime();
        } else if (flag == 1) {
            //凌晨23:59:59
            cal.setTimeInMillis(cal.getTimeInMillis() + 23 * 60 * 60 * 1000 + 59 * 60 * 1000 + 59 * 1000);
        }
        return cal.getTime();
    }


    public static Double hourDiff(Date now, Date lastShipTime) {
        double diff = lastShipTime.getTime() - now.getTime();
        double hourTime = 1000 * 60 * 60d;
        double hour = diff / hourTime;
        BigDecimal bg = new BigDecimal(hour);
        return bg.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    public static Date getAssignDaysAgo(Date date, int days) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_MONTH, -days);
        return cal.getTime();
    }

    public static Date getSevenDayskAgo(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_MONTH, -7);
        return cal.getTime();
    }

    public static Date getSevenDaysAfter(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_MONTH, 7);
        return cal.getTime();
    }

    public static Date getOneDayAgo(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_MONTH, -1);
        return cal.getTime();
    }


    public static Date getOneHourAgo() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.HOUR_OF_DAY, -1);
        return cal.getTime();
    }

    /**
     * 获取两个日期之间的间隔天数
     */
    public static float getDayBetween(Date beginDate, Date endDate) {
        try {
            long interval = endDate.getTime() - beginDate.getTime(); //时间间隔
            return interval / (float) (24 * 60 * 60 * 1000);
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * 比较两个日期是否在同一天
     */
    public static boolean sameDate(Date d1, Date d2) {
        SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
        return fmt.format(d1).equals(fmt.format(d2));
    }

    public static int getBetweenDays(Date beginDate, Date endDate) {
        try {
            long interval = endDate.getTime() - beginDate.getTime(); //时间间隔
            return (int) (interval / (24 * 60 * 60 * 1000));
        } catch (Exception e) {
            return 0;
        }
    }

    public static Boolean isMatchLabourDay() throws Exception {
        Date now = new Date();
        return now.after(DateUtil.convertToDate("2018-06-12 00:00:00")) && now.before(DateUtil.convertToDate("2018-06-20 00:00:00"));
    }

    public static Date getYesterday() throws Exception {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.DAY_OF_MONTH, -1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        return cal.getTime();
    }

    public static Date getToday() throws Exception {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        return cal.getTime();
    }

    public static String formatDay(Date date) throws Exception {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateFormat.format(date);
    }

    public static String getSecondTime(Date date) throws Exception {
        return String.valueOf(date.getTime() / 1000);
    }


    public static Boolean checkTimeInterval(Date startDate, Date endDate, int day) {
        long startTime = startDate.getTime();
        long endTime = endDate.getTime();
        if (endTime - startTime > day * OneDayTimeNumber) {
            return Boolean.FALSE;
        }
        return Boolean.TRUE;
    }

    /**
     * 转化成时间戳
     *
     * @param time 未知时间
     */
    public static Long checkTimeMillis(Long time) {
        time = Math.abs(time);
        long num = (long) Math.pow(10, 13 - String.valueOf(time).length());
        return time * num;
    }

    public static Date getTime(long payTime) {
        if (payTime == 0) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(new Date());
            cal.add(Calendar.DAY_OF_MONTH, 30);
            return cal.getTime();
        }
        long timestamp = DateUtil.checkTimeMillis(payTime);
        try {
            return DateUtil.convertToDate(timestamp);
        } catch (Exception e) {
        }
        return null;
    }

    /**
     * 将日期转化成10位秒级时间戳  （团好货需求）
     *
     * @return 示例：1607654748
     */
    public static int convertDateToMillis(Date date) {
        Long timestamp = date.getTime();
        return (int) (timestamp / 1000);
    }


    public static void main(String[] args) throws Exception {
        Date startDate = convertToDate("2023-09-07 10:03:32");
        Date endDate = convertToDate("2019-10-21 00:00:00");
//        System.out.println(endDate.getTime()-startDate.getTime());
//        System.out.println(checkTimeInterval(startDate,endDate,30));
//        System.out.println(addSecond(startDate,10*60));

        System.out.println(convertToDate(1599807821019L));
        System.out.println(new Date(1599807821019L));
        System.out.println(getBetweenDays(new Date(),startDate));
		System.out.println(new Date().after(DateUtil.convertToDate("2023-08-17 00:00:00")));
		System.out.println(new Date().before(DateUtil.convertToDate("2023-08-17 23:59:59")));
		System.out.println(!Pattern.matches("^1[3-9]\\d{9}$\n", "18163524265"));
    }

    /**
     * 获取当天开始时间
     */
    public static Date getTodayStart(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }

    /**
     * 获取当天结束时间
     */
    public static Date getTodayEnd(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        c.set(Calendar.MILLISECOND, 999);
        return c.getTime();
    }


}
